package com.qc31.comlib.choose.team

import android.app.Activity
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.qc31.baselib.base.LazyLoadSirFragment
import com.qc31.baselib.loadsie.EmptyCallback
import com.qc31.baselib.utils.isFastClick
import com.qc31.baselib.utils.setResultTo
import com.qc31.baselib.utils.toast
import com.qc31.comlib.Keys
import com.qc31.comlib.R
import com.qc31.comlib.adapter.ChooseTeamAdapter
import com.qc31.comlib.databinding.FragmentSearchRecyclerBinding

/** @Author Jasper @Time 2021/5/14 16:00 @Desc */
class SearchTeamFragment :
    LazyLoadSirFragment<FragmentSearchRecyclerBinding>(FragmentSearchRecyclerBinding::inflate) {
    private var isMap: Boolean = false
    private var isChoose: Boolean = false

    private val mAdapter by lazy { ChooseTeamAdapter() }

    val mViewModel by activityViewModels<ChooseTeamViewModel>()

    override fun onAttach(activity: Activity) {
        super.onAttach(activity)
        if (activity is ChooseTeamActivity) {
            isMap = activity.intent.getBooleanExtra("isMap", false)
            isChoose = activity.intent.getBooleanExtra("isChoose", false)
            mAdapter.isChoose = isChoose
        }
    }

    override fun initView(view: View) {
        initRecyclerView()
    }

    override fun initData() {
        loadService.showCallback(EmptyCallback::class.java)
        mViewModel.searchTeamsSub.hide().autoDispose(scopeProvider).subscribe {
            if (it.isEmpty()) {
                R.string.toast_no_find_team.toast(requireContext())
            } else {
                loadService.showSuccess()
                mAdapter.setList(it)
            }
        }
        mViewModel.searchTeam("")
    }

    override fun initEvent() {
        binding.tvFilter.setOnClickListener {
            if (isFastClick()) return@setOnClickListener
            mViewModel.searchTeam(binding.editSearch.text.toString())
        }
    }

    private fun initRecyclerView() {
        binding.includeRecycler.recyclerView.layoutManager = LinearLayoutManager(requireContext())
//        emptyView.findViewById<TextView>(R.id.tv_empty).setText(R.string.toast_no_team)
//        mAdapter.setEmptyView(emptyView)
        binding.includeRecycler.recyclerView.adapter = mAdapter.apply {
            setOnItemChildClickListener { adapter, view, position ->
                if (isFastClick()) return@setOnItemChildClickListener
                val teamInfo = data[position]
                if (!isChoose && teamInfo.carNum <= 0) {
                    R.string.toast_choose_no_car.toast(requireContext())
                    return@setOnItemChildClickListener
                }

                if (isMap && teamInfo.carNum > 500) {
                    R.string.toast_team_cars_much.toast(requireContext())
                    return@setOnItemChildClickListener
                }
                setResultTo(requireActivity()) {
                    putExtra(Keys.INTENT_TEAM_ID, teamInfo.teamId)
                    putExtra(Keys.INTENT_TEAM_NUM, teamInfo.carNum.toString())
                    putExtra(Keys.INTENT_TEAM_NAME, teamInfo.teamName)
                }
                requireActivity().finish()
            }

        }
    }

//    //空布局
//    private val emptyView by lazy {
//        layoutInflater.inflate(
//            R.layout.layout_empty_view,
//            binding.includeRecycler.recyclerView,
//            false
//        )
//    }
}